package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerManager;

public class CustomerManagerImpl implements CustomerManager {
	private List<Customer> customers = new ArrayList<Customer>();

	@Override
	public Customer addCustomer(Customer c) {
		synchronized (customers) {
			c.setId(UUID.randomUUID().toString());
			customers.add(c);
		}
		return c;
	}

	@Override
	public Customer deleteCustomer(Customer c) {
		for(Customer customer:customers){
			if(customer.getId().equals(c.getId())){
				customers.remove(customer);
				break;
			}
		}
		return c;
	}

	@Override
	public Customer updateCustomer(Customer c) {
		Customer ct = null;
		for(Customer customer:customers){
			if(customer.getId().equals(c.getId())){
				customers.remove(customer);
				customers.add(c);
				break;
			}
		}
		return ct;
	}

	@Override
	public List<Customer> findAll() {
		return customers;
	}

	@Override
	public Customer findCustomer(Customer c) {
		Customer ct = null;
		for(Customer customer:customers){
			if(customer.getId().equals(c.getId())){
				ct = customer;
				break;
			}
		}
		return ct;
	}

}
